package com.brillilab.service.core.comments.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.comments.CommentsMapper;
import com.brillilab.domain.enums.comments.CommentsClassifyEnum;
import com.brillilab.domain.po.comments.Comments;
import com.brillilab.domain.vo.comments.CommentsPagePaVo;
import com.brillilab.service.core.comments.ICommentsService;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

@Service
@Transactional
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements ICommentsService {

	@Resource
	private CommentsMapper commentsMapper;

	@Override
	public int getCommentNum(Integer classify, Long classifyId, Integer type) {
		LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<Comments>();
		wrapper.eq(Comments::getClassify, classify);
		wrapper.eq(Comments::getClassifyId, classifyId);
		if (type != null) {
			wrapper.eq(Comments::getType, type);
		}
		return commentsMapper.selectCount(wrapper);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public List<Comments> getCommentList(Integer classify, Long classifyId, Integer type) {
		LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<Comments>();
		wrapper.eq(Comments::getClassify, classify);
		wrapper.eq(Comments::getClassifyId, classifyId);
		if (type != null) {
			wrapper.eq(Comments::getType, type);
		}
		wrapper.orderByDesc(Comments::getId);
		return commentsMapper.selectList(wrapper);
	}

	@Override
	@SuppressWarnings("unchecked")
	public PageVo<Comments> getCommentPage(CommentsPagePaVo paVo) {
		LambdaQueryWrapper<Comments> wrapper = new LambdaQueryWrapper<Comments>();
		wrapper.eq(Comments::getClassify, paVo.getClassify());
		wrapper.eq(Comments::getClassifyId, paVo.getClassifyId());
		if (paVo.getType() != null) {
			wrapper.eq(Comments::getType, paVo.getType());
		}
		wrapper.orderByDesc(Comments::getId);
		IPage<Comments> iPage = new Page<Comments>(paVo.getPageNum(),paVo.getPageSize());
		IPage<Comments> page = commentsMapper.selectPage(iPage, wrapper);
		return new PageVo<Comments>(page);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Comments> getPlanList(Long planMemberId, Date startDate, Date endDate) {
		DateTime startTime = DateUtil.beginOfDay(startDate);
		DateTime endTime = DateUtil.endOfDay(endDate);
		LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Comments::getClassify, CommentsClassifyEnum.PLAN.getValue());
		queryWrapper.eq(Comments::getClassifyId, planMemberId);
		queryWrapper.ge(Comments::getRemark, String.valueOf(startTime.getTime()));
		queryWrapper.le(Comments::getRemark, String.valueOf(endTime.getTime()));
		queryWrapper.orderByDesc(Comments::getCreateTime);
		return commentsMapper.selectList(queryWrapper);
	}

	@Override
	public int planNum(Long labMemberId, Long planMemberId, DateTime startDate, DateTime endDate) {
		DateTime startTime = DateUtil.beginOfDay(startDate);
		DateTime endTime = DateUtil.endOfDay(endDate);
		LambdaQueryWrapper<Comments> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(Comments::getClassify, CommentsClassifyEnum.PLAN.getValue());
		queryWrapper.eq(Comments::getClassifyId, planMemberId);
		queryWrapper.ge(Comments::getRemark, String.valueOf(startTime.getTime()));
		queryWrapper.le(Comments::getRemark, String.valueOf(endTime.getTime()));
		return commentsMapper.selectCount(queryWrapper);
	}

}
